﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using YArchitech.LIB;
using HYElectricCommon;

namespace YArchitech.Revit.MEP.Calculator
{
	public partial class DlgColumnModify : System.Windows.Forms.Form
	{
		public DlgColumnModify()
		{
			this.InitializeComponent();
			this.DicRoomData = new Dictionary<string, string>();
			this.InitCmbWallReflectionFactor();
			this.InitCmbGroundReflectionFactor();
			this.BindEvent();
		}

		private void DlgColumnModify_Load(object sender, EventArgs e)
		{
			this.LoadDlgData();
			//base.Icon = Resource.YArchitechIcon;
			string str = Path.Combine(Product.WorkingLocation, "IlluminanceCal.db3");
			SqLiteHelper.SqlLiteDatabase("Data Source=" + str + ";Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10");
		}

		private void BindEvent()
		{
			this.chkRoomHeight.CheckedChanged += this.checkbox_CheckedChanged;
			this.chkWorkHeight.CheckedChanged += this.checkbox_CheckedChanged;
			this.chkInstallHeight.CheckedChanged += this.checkbox_CheckedChanged;
			this.chkCeilingFactor.CheckedChanged += this.checkbox_CheckedChanged;
			this.chkWallUpFactor.CheckedChanged += this.checkbox_CheckedChanged;
			this.chkWallDownFactor.CheckedChanged += this.checkbox_CheckedChanged;
			this.chkFloorFactor.CheckedChanged += this.checkbox_CheckedChanged;
			this.chkWindowFactor.CheckedChanged += this.checkbox_CheckedChanged;
			this.chkLux.CheckedChanged += this.checkbox_CheckedChanged;
			this.chkLpd.CheckedChanged += this.checkbox_CheckedChanged;
		}

		private void checkbox_CheckedChanged(object sender, EventArgs e)
		{
			CheckBox checkBox = sender as CheckBox;
			string name = checkBox.Name;
			uint num = 0;//<PrivateImplementationDetails>.ComputeStringHash(name);
			if (num <= 1874330477u)
			{
				if (num <= 995222776u)
				{
					if (num != 120679171u)
					{
						if (num != 995222776u)
						{
							return;
						}
						if (!(name == "chkWallDownFactor"))
						{
							return;
						}
						this.BindEvent(checkBox, this.cmbWallDownFactor);
						return;
					}
					else
					{
						if (!(name == "chkRoomHeight"))
						{
							return;
						}
						this.BindEvent(checkBox, this.tbRoomHeight);
						return;
					}
				}
				else if (num != 1482031017u)
				{
					if (num != 1816345252u)
					{
						if (num != 1874330477u)
						{
							return;
						}
						if (!(name == "chkCeilingFactor"))
						{
							return;
						}
						this.BindEvent(checkBox, this.cmbCeilingFactor);
						return;
					}
					else
					{
						if (!(name == "chkLux"))
						{
							return;
						}
						this.BindEvent(checkBox, this.btnLux);
						this.BindEvent(checkBox, this.tbLux);
						return;
					}
				}
				else
				{
					if (!(name == "chkWallUpFactor"))
					{
						return;
					}
					this.BindEvent(checkBox, this.cmbWallUpFactor);
					return;
				}
			}
			else if (num <= 2287064048u)
			{
				if (num != 2252018893u)
				{
					if (num != 2287064048u)
					{
						return;
					}
					if (!(name == "chkWindowFactor"))
					{
						return;
					}
					this.BindEvent(checkBox, this.tbWindowFactor);
					return;
				}
				else
				{
					if (!(name == "chkLpd"))
					{
						return;
					}
					this.BindEvent(checkBox, this.btnLpd);
					this.BindEvent(checkBox, this.tbLpd);
					return;
				}
			}
			else if (num != 2458272109u)
			{
				if (num != 3750759676u)
				{
					if (num != 4114300233u)
					{
						return;
					}
					if (!(name == "chkWorkHeight"))
					{
						return;
					}
					this.BindEvent(checkBox, this.tbWorkHeight);
					return;
				}
				else
				{
					if (!(name == "chkFloorFactor"))
					{
						return;
					}
					this.BindEvent(checkBox, this.cmbFloorFactor);
					return;
				}
			}
			else
			{
				if (!(name == "chkInstallHeight"))
				{
					return;
				}
				this.BindEvent(checkBox, this.tbInstallHeight);
				return;
			}
		}

		private void BindEvent(CheckBox chk, Control ctl)
		{
			if (chk.Checked)
			{
				ctl.Enabled = true;
				return;
			}
			ctl.Enabled = false;
		}

		private void GetLuxAndLpdValues()
		{
			using (DlgSelIllAndPower dlgSelIllAndPower = new DlgSelIllAndPower())
			{
				if (dlgSelIllAndPower.ShowDialog() == DialogResult.OK)
				{
					try
					{
						this.tbLux.Text = dlgSelIllAndPower.m_IllAndPowerData.IllStandard.ToString();
						this.tbLpd.Text = dlgSelIllAndPower.m_IllAndPowerData.NowPD.ToString();
					}
					catch (Exception)
					{
					}
				}
			}
			string str = Path.Combine(Product.WorkingLocation, "IlluminanceCal.db3");
			SqLiteHelper.SqlLiteDatabase("Data Source=" + str + ";Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10");
		}

		private void InitCmbWallReflectionFactor()
		{
			this.cmbCeilingFactor.Items.Clear();
			this.cmbWallDownFactor.Items.Clear();
			this.cmbWallUpFactor.Items.Clear();
			System.Data.DataTable dataTable = Database.ExecQuery("select [RefFactor], [Describe] from [CeilingRef]");
			if (dataTable != null)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					ReflectionFactorData reflectionFactorData = new ReflectionFactorData();
					reflectionFactorData.Describe = dataTable.Rows[i][1].ToString() + " :" + dataTable.Rows[i][0].ToString();
					reflectionFactorData.Value = Convert.ToDouble(dataTable.Rows[i][0].ToString());
					this.cmbCeilingFactor.Items.Add(reflectionFactorData);
					this.cmbWallDownFactor.Items.Add(reflectionFactorData);
					this.cmbWallUpFactor.Items.Add(reflectionFactorData);
				}
			}
			if (this.cmbCeilingFactor.Items.Count > 0)
			{
				this.cmbCeilingFactor.SelectedIndex = 0;
			}
			if (this.cmbWallDownFactor.Items.Count > 0)
			{
				this.cmbWallDownFactor.SelectedIndex = 0;
			}
			if (this.cmbWallUpFactor.Items.Count > 0)
			{
				this.cmbWallUpFactor.SelectedIndex = 0;
			}
		}

		private void InitCmbGroundReflectionFactor()
		{
			this.cmbFloorFactor.Items.Clear();
			System.Data.DataTable dataTable = Database.ExecQuery("select [RefFactor], [Describe] from [GroundRef]");
			if (dataTable != null)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					ReflectionFactorData reflectionFactorData = new ReflectionFactorData();
					reflectionFactorData.Describe = dataTable.Rows[i][1].ToString() + " :" + dataTable.Rows[i][0].ToString();
					reflectionFactorData.Value = Convert.ToDouble(dataTable.Rows[i][0].ToString());
					this.cmbFloorFactor.Items.Add(reflectionFactorData);
				}
			}
			if (this.cmbFloorFactor.Items.Count > 0)
			{
				this.cmbFloorFactor.SelectedIndex = 0;
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.DicRoomData.Clear();
			string text = "房间高度、安装高度和工作面高度的参数值应为：\"房间高度 > 安装高度 > 工作面高度\",请修改。";
			double num = 0.0;
			if (this.chkRoomHeight.Checked)
			{
				if (string.IsNullOrEmpty(this.tbRoomHeight.Text))
				{
					MessageBox.Show(this.chkRoomHeight.Text + "不能为空。", "盈建科提示");
					this.chkRoomHeight.Focus();
					return;
				}
				if (!double.TryParse(this.tbRoomHeight.Text, out num))
				{
					MessageBox.Show(this.chkRoomHeight.Text + "输入不合法，请重新输入。", "盈建科提示");
					this.chkRoomHeight.Focus();
					return;
				}
				if (!this.ValidRoomHeight(this.tbRoomHeight))
				{
					MessageBox.Show(text, "盈建科提示");
					this.tbRoomHeight.Focus();
					return;
				}
				this.DicRoomData.Add("RoomHeight", this.tbRoomHeight.Text);
			}
			if (this.chkWorkHeight.Checked)
			{
				if (string.IsNullOrEmpty(this.tbWorkHeight.Text))
				{
					MessageBox.Show(this.chkWorkHeight.Text + "不能为空。", "盈建科提示");
					this.tbWorkHeight.Focus();
					return;
				}
				if (!double.TryParse(this.tbWorkHeight.Text, out num))
				{
					MessageBox.Show(this.chkWorkHeight.Text + "输入不合法，请重新输入。", "盈建科提示");
					this.tbWorkHeight.Focus();
					return;
				}
				if (!this.ValidWorkHeight(this.tbWorkHeight))
				{
					MessageBox.Show(text, "盈建科提示");
					this.tbWorkHeight.Focus();
					return;
				}
				this.DicRoomData.Add("WorkHeight", this.tbWorkHeight.Text);
			}
			if (this.chkInstallHeight.Checked)
			{
				if (string.IsNullOrEmpty(this.tbInstallHeight.Text))
				{
					MessageBox.Show(this.chkInstallHeight.Text + "不能为空。", "盈建科提示");
					this.tbInstallHeight.Focus();
					return;
				}
				if (!double.TryParse(this.tbInstallHeight.Text, out num))
				{
					MessageBox.Show(this.chkInstallHeight.Text + "输入不合法，请重新输入。", "盈建科提示");
					this.tbInstallHeight.Focus();
					return;
				}
				if (!this.ValidInstallHeight(this.tbInstallHeight))
				{
					MessageBox.Show(text, "盈建科提示");
					this.tbInstallHeight.Focus();
					return;
				}
				this.DicRoomData.Add("InstallHeight", this.tbInstallHeight.Text);
			}
			if (this.chkWindowFactor.Checked)
			{
				if (string.IsNullOrEmpty(this.tbWindowFactor.Text))
				{
					MessageBox.Show(this.chkWindowFactor.Text + "不能为空。", "盈建科提示");
					this.tbWindowFactor.Focus();
					return;
				}
				if (!double.TryParse(this.tbWindowFactor.Text, out num))
				{
					MessageBox.Show(this.chkWindowFactor.Text + "输入不合法，请重新输入。", "盈建科提示");
					this.tbWindowFactor.Focus();
					return;
				}
				this.DicRoomData.Add("WindowFactor", this.tbWindowFactor.Text);
			}
			if (this.cmbCeilingFactor.SelectedIndex >= 0 && this.chkCeilingFactor.Checked)
			{
				this.DicRoomData.Add("CeilingFactor", ((ReflectionFactorData)this.cmbCeilingFactor.SelectedItem).Value.ToString());
			}
			if (this.cmbWallUpFactor.SelectedIndex >= 0 && this.chkWallUpFactor.Checked)
			{
				this.DicRoomData.Add("WallUpFactor", ((ReflectionFactorData)this.cmbWallUpFactor.SelectedItem).Value.ToString());
			}
			if (this.cmbWallDownFactor.SelectedIndex >= 0 && this.chkWallDownFactor.Checked)
			{
				this.DicRoomData.Add("WallDownFactor", ((ReflectionFactorData)this.cmbWallDownFactor.SelectedItem).Value.ToString());
			}
			if (this.cmbFloorFactor.SelectedIndex >= 0 && this.chkFloorFactor.Checked)
			{
				this.DicRoomData.Add("FloorFactor", ((ReflectionFactorData)this.cmbFloorFactor.SelectedItem).Value.ToString());
			}
			if (this.chkLux.Checked)
			{
				if (string.IsNullOrEmpty(this.tbLux.Text))
				{
					MessageBox.Show(this.chkLux.Text + "不能为空。", "盈建科提示");
					this.tbLux.Focus();
					return;
				}
				if (!double.TryParse(this.tbLux.Text, out num))
				{
					MessageBox.Show(this.chkLux.Text + "输入不合法，请重新输入。", "盈建科提示");
					this.tbLux.Focus();
					return;
				}
				this.DicRoomData.Add("Lux", this.tbLux.Text);
			}
			if (this.chkLpd.Checked)
			{
				if (string.IsNullOrEmpty(this.tbLpd.Text))
				{
					MessageBox.Show(this.chkLpd.Text + "不能为空。", "盈建科提示");
					this.tbLpd.Focus();
					return;
				}
				if (!double.TryParse(this.tbLpd.Text, out num))
				{
					MessageBox.Show(this.chkLpd.Text + "输入不合法，请重新输入。", "盈建科提示");
					this.tbLpd.Focus();
					return;
				}
				this.DicRoomData.Add("Lpd", this.tbLpd.Text);
			}
			base.DialogResult = DialogResult.OK;
		}

		private bool ValidRoomHeight(TextBox txt)
		{
			if (!this.chkInstallHeight.Checked && !this.chkWorkHeight.Checked)
			{
				return true;
			}
			if (this.chkInstallHeight.Checked && !this.chkWorkHeight.Checked)
			{
				string value = this.tbRoomHeight.Text.Trim();
				string value2 = this.tbInstallHeight.Text.Trim();
				if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(value2) && Convert.ToDouble(value) < Convert.ToDouble(value2))
				{
					return false;
				}
			}
			else if (!this.chkInstallHeight.Checked && this.chkWorkHeight.Checked)
			{
				string value3 = this.tbRoomHeight.Text.Trim();
				string value4 = this.tbWorkHeight.Text.Trim();
				if (!string.IsNullOrEmpty(value3) && !string.IsNullOrEmpty(value4) && Convert.ToDouble(value3) < Convert.ToDouble(value4))
				{
					return false;
				}
			}
			else
			{
				string value5 = this.tbRoomHeight.Text.Trim();
				string value6 = this.tbWorkHeight.Text.Trim();
				string value7 = this.tbInstallHeight.Text.Trim();
				if (!string.IsNullOrEmpty(value5) && !string.IsNullOrEmpty(value7) && Convert.ToDouble(value5) < Convert.ToDouble(value7))
				{
					return false;
				}
				if (!string.IsNullOrEmpty(value5) && !string.IsNullOrEmpty(value6) && Convert.ToDouble(value5) < Convert.ToDouble(value6))
				{
					return false;
				}
			}
			return true;
		}

		private bool ValidInstallHeight(TextBox txt)
		{
			if (!this.chkRoomHeight.Checked && !this.chkWorkHeight.Checked)
			{
				return true;
			}
			if (this.chkRoomHeight.Checked && !this.chkWorkHeight.Checked)
			{
				string value = this.tbInstallHeight.Text.Trim();
				string value2 = this.tbRoomHeight.Text.Trim();
				if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(value2) && Convert.ToDouble(value) > Convert.ToDouble(value2))
				{
					return false;
				}
			}
			else if (!this.chkRoomHeight.Checked && this.chkWorkHeight.Checked)
			{
				string value3 = this.tbInstallHeight.Text.Trim();
				string value4 = this.tbWorkHeight.Text.Trim();
				if (!string.IsNullOrEmpty(value3) && !string.IsNullOrEmpty(value4) && Convert.ToDouble(value3) < Convert.ToDouble(value4))
				{
					return false;
				}
			}
			else
			{
				string value5 = this.tbRoomHeight.Text.Trim();
				string value6 = this.tbWorkHeight.Text.Trim();
				string value7 = this.tbInstallHeight.Text.Trim();
				if (!string.IsNullOrEmpty(value7) && !string.IsNullOrEmpty(value5) && Convert.ToDouble(value7) > Convert.ToDouble(value5))
				{
					return false;
				}
				if (!string.IsNullOrEmpty(value7) && !string.IsNullOrEmpty(value6) && Convert.ToDouble(value7) < Convert.ToDouble(value6))
				{
					return false;
				}
			}
			return true;
		}

		private bool ValidWorkHeight(TextBox txt)
		{
			if (!this.chkRoomHeight.Checked && !this.chkInstallHeight.Checked)
			{
				return true;
			}
			if (this.chkRoomHeight.Checked && !this.chkInstallHeight.Checked)
			{
				string value = this.tbWorkHeight.Text.Trim();
				string value2 = this.tbRoomHeight.Text.Trim();
				if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(value2) && Convert.ToDouble(value) > Convert.ToDouble(value2))
				{
					return false;
				}
			}
			if (!this.chkRoomHeight.Checked && this.chkInstallHeight.Checked)
			{
				string value3 = this.tbInstallHeight.Text.Trim();
				string value4 = this.tbWorkHeight.Text.Trim();
				if (!string.IsNullOrEmpty(value4) && !string.IsNullOrEmpty(value3) && Convert.ToDouble(value4) > Convert.ToDouble(value3))
				{
					return false;
				}
			}
			else
			{
				string value5 = this.tbRoomHeight.Text.Trim();
				string value6 = this.tbWorkHeight.Text.Trim();
				string value7 = this.tbInstallHeight.Text.Trim();
				if (!string.IsNullOrEmpty(value6) && !string.IsNullOrEmpty(value5) && Convert.ToDouble(value6) > Convert.ToDouble(value5))
				{
					return false;
				}
				if (!string.IsNullOrEmpty(value6) && !string.IsNullOrEmpty(value7) && Convert.ToDouble(value6) > Convert.ToDouble(value7))
				{
					return false;
				}
			}
			return true;
		}

		private void tbRoomHeight_KeyPress(object sender, KeyPressEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			this.Validata(textBox.Text.Trim(), e);
		}

		private void Validata(string txt, KeyPressEventArgs e)
		{
			if (e.KeyChar == ' ')
			{
				e.Handled = true;
				return;
			}
			if (e.KeyChar == '\b')
			{
				return;
			}
			double num = 0.0;
			if (!double.TryParse(txt + e.KeyChar.ToString(), out num))
			{
				e.Handled = true;
				return;
			}
			if (num >= 0.0)
			{
				e.Handled = false;
				return;
			}
			e.Handled = true;
		}

		private void btnLux_Click(object sender, EventArgs e)
		{
			this.GetLuxAndLpdValues();
		}

		private void btnLpd_Click(object sender, EventArgs e)
		{
			this.GetLuxAndLpdValues();
		}

		private void SaveDlgData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "ColumnModify.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			string value = this.chkRoomHeight.Checked.ToString();
			streamWriter.WriteLine(value);
			value = this.tbRoomHeight.Text;
			streamWriter.WriteLine(value);
			value = this.chkWorkHeight.Checked.ToString();
			streamWriter.WriteLine(value);
			value = this.tbWorkHeight.Text;
			streamWriter.WriteLine(value);
			value = this.chkInstallHeight.Checked.ToString();
			streamWriter.WriteLine(value);
			value = this.tbInstallHeight.Text;
			streamWriter.WriteLine(value);
			value = this.chkCeilingFactor.Checked.ToString();
			streamWriter.WriteLine(value);
			value = this.cmbCeilingFactor.SelectedIndex.ToString();
			streamWriter.WriteLine(value);
			value = this.chkWallUpFactor.Checked.ToString();
			streamWriter.WriteLine(value);
			value = this.cmbWallUpFactor.SelectedIndex.ToString();
			streamWriter.WriteLine(value);
			value = this.chkWallDownFactor.Checked.ToString();
			streamWriter.WriteLine(value);
			value = this.cmbWallDownFactor.SelectedIndex.ToString();
			streamWriter.WriteLine(value);
			value = this.chkFloorFactor.Checked.ToString();
			streamWriter.WriteLine(value);
			value = this.cmbFloorFactor.SelectedIndex.ToString();
			streamWriter.WriteLine(value);
			value = this.chkWindowFactor.Checked.ToString();
			streamWriter.WriteLine(value);
			value = this.tbWindowFactor.Text;
			streamWriter.WriteLine(value);
			value = this.chkLux.Checked.ToString();
			streamWriter.WriteLine(value);
			value = this.tbLux.Text;
			streamWriter.WriteLine(value);
			value = this.chkLpd.Checked.ToString();
			streamWriter.WriteLine(value);
			value = this.tbLpd.Text;
			streamWriter.WriteLine(value);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void LoadDlgData()
		{
			string path = Path.GetTempPath() + "ColumnModify.dat";
			if (!File.Exists(path))
			{
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			string text = streamReader.ReadLine();
			this.chkRoomHeight.Checked = Convert.ToBoolean(text);
			text = streamReader.ReadLine();
			this.tbRoomHeight.Text = text;
			text = streamReader.ReadLine();
			this.chkWorkHeight.Checked = Convert.ToBoolean(text);
			text = streamReader.ReadLine();
			this.tbWorkHeight.Text = text;
			text = streamReader.ReadLine();
			this.chkInstallHeight.Checked = Convert.ToBoolean(text);
			text = streamReader.ReadLine();
			this.tbInstallHeight.Text = text;
			text = streamReader.ReadLine();
			this.chkCeilingFactor.Checked = Convert.ToBoolean(text);
			text = streamReader.ReadLine();
			this.cmbCeilingFactor.SelectedIndex = Convert.ToInt32(text);
			text = streamReader.ReadLine();
			this.chkWallUpFactor.Checked = Convert.ToBoolean(text);
			text = streamReader.ReadLine();
			this.cmbWallUpFactor.SelectedIndex = Convert.ToInt32(text);
			text = streamReader.ReadLine();
			this.chkWallDownFactor.Checked = Convert.ToBoolean(text);
			text = streamReader.ReadLine();
			this.cmbWallDownFactor.SelectedIndex = Convert.ToInt32(text);
			text = streamReader.ReadLine();
			this.chkFloorFactor.Checked = Convert.ToBoolean(text);
			text = streamReader.ReadLine();
			this.cmbFloorFactor.SelectedIndex = Convert.ToInt32(text);
			text = streamReader.ReadLine();
			this.chkWindowFactor.Checked = Convert.ToBoolean(text);
			text = streamReader.ReadLine();
			this.tbWindowFactor.Text = text;
			text = streamReader.ReadLine();
			this.chkLux.Checked = Convert.ToBoolean(text);
			text = streamReader.ReadLine();
			this.tbLux.Text = text;
			text = streamReader.ReadLine();
			this.chkLpd.Checked = Convert.ToBoolean(text);
			text = streamReader.ReadLine();
			this.tbLpd.Text = text;
			streamReader.Close();
			fileStream.Close();
		}

		private void DlgColumnModify_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
		}

		public Dictionary<string, string> DicRoomData;
	}
}
